package com.yanyeori.rabbitmqprovider.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chenkuan
 * @version v1.0
 * @desc 配置RabbitTemplate发送回调函数
 * @date 20/7/30 030 17:38
 */
@Configuration
public class RabbitTemplateConfig {
	private static final Logger logger = LoggerFactory.getLogger(RabbitTemplateConfig.class);

	@Bean
	public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
		RabbitTemplate rabbitTemplate = new RabbitTemplate();
		rabbitTemplate.setConnectionFactory(connectionFactory);
		//设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数
		rabbitTemplate.setMandatory(true);
		rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
			logger.debug("ConfirmCallback=相关数据：{}，确认情况：{}，原因：{}", correlationData, ack, cause);
		});
		rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
			logger.debug("ReturnCallback=消息：{}，回应码：{}，回应信息：{}，交换机：{}，路由键：{}", message, replyCode, replyText, exchange, routingKey);
		});
		return rabbitTemplate;
	}
}
